import React, { useState } from 'react';
// import { format } from '@/utils/datetime';
import regs from '@/utils/regs';
import CommonPage from '@/components/page/CommonPage';
import { initPassword, leaveOffice } from '@/services/user';
import { Button, message, Modal } from 'antd';
import IconFont from '@/components/base/IconFont';
import { useModel } from 'umi';
const daoName = 'User';
const importName = 'User';
const columns_nj: API.Column[] = [
  {
    title: '员工编号',
    dataIndex: 'No',
    key: 'No',
    width: 120,
    sorter: true,
    dispaly: true,
    filter: true,
  },
  {
    title: '员工名称',
    width: 150,
    sorter: true,
    dataIndex: 'Name',
    key: 'Name',
    dispaly: true,
    filter: true,
  },
  {
    title: '所属部门',
    width: 200,
    sorter: true,
    key: 'OrganizationID',
    dataIndex: ['Organization', 'Name'],
    dispaly: true,
    filter: true,
  },
  {
    title: '职位',
    width: 120,
    sorter: true,
    key: 'PositionID',
    dataIndex: ['Position', 'Name'],
    dispaly: true,
    filter: true,
  },
  {
    title: '序列',
    width: 150,
    sorter: true,
    key: 'SerialID',
    dataIndex: ['Serial', 'Name'],
    dispaly: true,
    filter: true,
  },
  {
    title: '邮箱',
    width: 140,
    sorter: true,
    dataIndex: 'EMail',
    key: 'EMail',
    dispaly: true,
    filter: true,
    canEdit: true,
    ellipsis: true,
  },
  {
    title: '公共户',
    width: 110,
    sorter: true,
    key: 'IsPublic',
    dataIndex: 'IsPublic',
    render: (data: any) => (data ? '是' : '否'),
    filter: true,
    dispaly: true,
  },
  {
    title: '零售管户人',
    width: 150,
    sorter: true,
    key: 'ISLSGHR',
    dataIndex: 'ISLSGHR',
    render: (data: any) => (data ? '是' : '否'),
    filter: true,
    dispaly: true,
  },
  {
    title: '状态',
    width: 100,
    sorter: true,
    key: 'STATUS',
    dataIndex: 'STATUSCaption',
    filter: true,
    dispaly: true,
  },
  {
    title: '系统角色',
    width: 200,
    sorter: true,
    dataIndex: 'RoleCaptions',
    key: 'RoleIds',
    filter: false,
    dispaly: true,
  },
];
const controls_nj: API.Control[] = [
  {
    name: 'No',
    type: 'text',
    label: '员工编号',
    grid: 12 / 24,
    rules: [
      { required: true, message: '员工编号不能为空' },
      { min: 6, max: 10, message: '长度不能小于6，大于10' },
      { pattern: regs.alphanum, message: '必须是字母或者数字' },
    ],
  },
  {
    name: 'Name',
    type: 'text',
    label: '员工名称',
    grid: 12 / 24,
    rules: [
      { required: true, message: '员工名称不能为空' },
      { min: 2, max: 10, message: '长度不能小于2，大于30' },
    ],
  },
  {
    name: 'OrganizationID',
    label: '所属部门',
    type: 'select',
    grid: 12 / 24,
    fetch: {
      tableName: 'Organization',
      optionTpl: 'Name',
      valueTpl: 'ID',
      parentTpl: 'OrganID',
      isDao: true,
    },
    dataType: 'number',
    placeholder: '请选择所属部门...',
    rules: [{ required: true, message: '所属部门不能为空' }],
  },
  {
    name: 'PositionID',
    label: '职位',
    type: 'select',
    grid: 12 / 24,
    dataType: 'number',
    fetch: { tableName: 'Position', optionTpl: 'Name', valueTpl: 'ID', isDao: true },
    rules: [{ required: true, message: '职位不能为空' }],
  },
  {
    name: 'SerialID',
    label: '序列',
    type: 'select',
    grid: 12 / 24,
    dataType: 'number',
    fetch: { tableName: 'Serial', optionTpl: 'Name', valueTpl: 'ID', isDao: true },
    rules: [{ required: true, message: '序列不能为空' }],
  },
  // { name: "ZYXZ", label: "职员性质", required: true, data: [{ value: '1', label: '正式' }, { value: '2', label: '派遣' }, { value: '3', label: '外包' }, { value: '4', label: '虚拟' }, { value: '5', label: '其他' }], grid: 12 / 24, type: "select" },
  {
    name: 'RoleIds',
    label: '系统角色',
    type: 'select',
    grid: 12 / 24,
    multiple: true,
    fetch: { tableName: 'Role', optionTpl: 'Name', valueTpl: 'ID', isDao: true },
  },
  {
    name: 'EMail',
    label: '邮箱',
    type: 'text',
    grid: 12 / 24,
    rules: [{ pattern: regs.email, message: '邮箱格式不正确' }],
  },
  {
    name: 'IsPublic',
    label: '公共户',
    type: 'radio-group',
    grid: 12 / 24,
    dataType: 'number',
    tooltip: '网点下没有认定的业绩会自动关联到其下的公共户',
    options: [
      { value: 1, label: '是' },
      { value: 0, label: '否' },
    ],
    rules: [{ required: true, message: '是否公共不能为空' }],
  },
  {
    name: 'ISLSGHR',
    label: '零售管户人',
    type: 'radio-group',
    grid: 12 / 24,
    dataType: 'number',
    options: [
      { value: 1, label: '是' },
      { value: 0, label: '否' },
    ],
    rules: [{ required: true, message: '是否零售管户人不能为空' }],
  },
  {
    name: 'STATUS',
    label: '状态',
    options: [
      { value: '1', label: '正常' },
      { value: '2', label: '已离职' },
      { value: '3', label: '暂停使用' },
    ],
    rules: [{ required: true, message: '状态不能为空' }],
    grid: 12 / 24,
    type: 'radio-group',
  },
];
const columns_sh: API.Column[] = [
  {
    title: '员工编号',
    dataIndex: 'No',
    key: 'No',
    width: 120,
    sorter: true,
    dispaly: true,
    filter: true,
  },
  {
    title: '员工名称',
    width: 150,
    sorter: true,
    dataIndex: 'Name',
    key: 'Name',
    dispaly: true,
    filter: true,
  },
  { title: '性别', width: 90, key: 'Sex', dataIndex: 'SexCaption', dispaly: true, filter: true },
  // {
  //   title: '生日',
  //   width: 100,
  //   sorter: true,
  //   key: 'Birthday',
  //   dataIndex: 'Birthday',
  //   render: (data: any) => format(data, 'yyyy-MM-dd'),
  //   dispaly: true,
  //   filter: true,
  //   canEdit: true,
  // },
  {
    title: '所属部门',
    width: 200,
    sorter: true,
    key: 'OrganizationID',
    dataIndex: ['Organization', 'Name'],
    dispaly: true,
    filter: true,
  },
  {
    title: '职位',
    width: 120,
    sorter: true,
    key: 'PositionID',
    dataIndex: ['Position', 'Name'],
    dispaly: true,
    filter: true,
  },
  {
    title: '序列',
    width: 150,
    sorter: true,
    key: 'SerialID',
    dataIndex: ['Serial', 'Name'],
    dispaly: true,
    filter: true,
  },
  // { title: '职员性质', width: 120, sorter: true, key: 'ZYXZ', dataIndex: 'ZYXZCaption', dispaly: true, filter: true },
  {
    title: '邮箱',
    width: 140,
    sorter: true,
    dataIndex: 'EMail',
    key: 'EMail',
    dispaly: true,
    filter: true,
    canEdit: true,
    ellipsis: true,
  },
  {
    title: '公共户',
    width: 110,
    sorter: true,
    key: 'IsPublic',
    dataIndex: 'IsPublic',
    render: (data: any) => (data ? '是' : '否'),
    filter: true,
    dispaly: true,
  },
  {
    title: '状态',
    width: 100,
    sorter: true,
    key: 'STATUS',
    dataIndex: 'STATUSCaption',
    filter: true,
    dispaly: true,
  },
  {
    title: '系统角色',
    width: 200,
    sorter: true,
    dataIndex: 'RoleCaptions',
    key: 'RoleIds',
    filter: false,
    dispaly: true,
  },
];
const controls_sh: API.Control[] = [
  {
    name: 'No',
    type: 'text',
    label: '员工编号',
    grid: 12 / 24,
    rules: [
      { required: true, message: '员工编号不能为空' },
      { min: 6, max: 10, message: '长度不能小于6，大于10' },
      { pattern: regs.alphanum, message: '必须是字母或者数字' },
    ],
  },
  {
    name: 'Name',
    type: 'text',
    label: '员工名称',
    grid: 12 / 24,
    rules: [
      { required: true, message: '员工名称不能为空' },
      { min: 2, max: 10, message: '长度不能小于2，大于30' },
    ],
  },
  {
    name: 'Sex',
    label: '性别',
    options: [
      { value: '1', label: '男' },
      { value: '2', label: '女' },
    ],
    grid: 12 / 24,
    type: 'radio-group',
    rules: [{ required: true, message: '性别不能为空' }],
  },
  // {
  //   name: 'Birthday',
  //   label: '生日',
  //   grid: 12 / 24,
  //   type: 'date',
  //   rules: [{ required: true, message: 'date不能为空' }],
  // },
  {
    name: 'OrganizationID',
    label: '所属部门',
    type: 'select',
    grid: 12 / 24,
    fetch: {
      tableName: 'Organization',
      optionTpl: 'Name',
      valueTpl: 'ID',
      parentTpl: 'OrganID',
      isDao: true,
    },
    dataType: 'number',
    placeholder: '请选择所属部门...',
    rules: [{ required: true, message: '所属部门不能为空' }],
  },
  // {
  //   name: "YWID", label: "所属支行条线", type: "tree-select", grid: 12 / 24, required: false,
  //   fetch: { daoName: "YWTX", optionTpl: 'Name', valueTpl: "ID" },
  //   dataType: "number", seekType: 2, placeholder: '请选择所属支行条线...', layout: { labelSpan: 8, wrapperSpan: 13 }
  // },
  {
    name: 'PositionID',
    label: '职位',
    type: 'select',
    grid: 12 / 24,
    dataType: 'number',
    fetch: { tableName: 'Position', optionTpl: 'Name', valueTpl: 'ID', isDao: true },
    rules: [{ required: true, message: '职位不能为空' }],
  },
  {
    name: 'SerialID',
    label: '序列',
    type: 'select',
    grid: 12 / 24,
    dataType: 'number',
    fetch: { tableName: 'Serial', optionTpl: 'Name', valueTpl: 'ID', isDao: true },
    rules: [{ required: true, message: '序列不能为空' }],
  },
  // { name: "ZYXZ", label: "职员性质", required: true, data: [{ value: '1', label: '正式' }, { value: '2', label: '派遣' }, { value: '3', label: '外包' }, { value: '4', label: '虚拟' }, { value: '5', label: '其他' }], grid: 12 / 24, type: "select" },
  {
    name: 'RoleIds',
    label: '系统角色',
    type: 'select',
    grid: 12 / 24,
    multiple: true,
    fetch: { tableName: 'Role', optionTpl: 'Name', valueTpl: 'ID', isDao: true },
  },
  {
    name: 'EMail',
    label: '邮箱',
    type: 'text',
    grid: 12 / 24,
    rules: [{ pattern: regs.email, message: '邮箱格式不正确' }],
  },
  {
    name: 'IsPublic',
    label: '公共户',
    type: 'radio-group',
    grid: 12 / 24,
    dataType: 'number',
    tooltip: '网点下没有认定的业绩会自动关联到其下的公共户',
    options: [
      { value: 1, label: '是' },
      { value: 0, label: '否' },
    ],
    rules: [{ required: true, message: '是否公共不能为空' }],
  },
  {
    name: 'STATUS',
    label: '状态',
    options: [
      { value: '1', label: '正常' },
      { value: '2', label: '已离职' },
      { value: '3', label: '暂停使用' },
    ],
    rules: [{ required: true, message: '状态不能为空' }],
    grid: 12 / 24,
    type: 'radio-group',
  },
];

let columns: API.Column[] = [];
let controls: API.Control[] = [];

const Users: React.FC = () => {
  // 从当前应用状态中获取appNo，并根据不同的appNo进行相应的赋值
  const { appNo } = useModel('@@initialState', (ret) => ({
    appNo: ret.initialState?.appNo,
  }));
  if (appNo === 'sh') {
    columns = columns_sh; // 当appNo为'sh'时，使用columns_sh作为columns
    controls = controls_sh; // 当appNo为'sh'时，使用controls_sh作为controls
  } else if (appNo === 'nj') {
    columns = columns_nj; // 当appNo为'nj'时，使用columns_nj作为columns
    controls = controls_nj; // 当appNo为'nj'时，使用controls_nj作为controls
  }

  // 使用useState hook来定义名为selKeys的状态变量和对应的更新函数setSelKeys
  const [selKeys, setSelKeys] = useState<React.Key[]>([]);

  // 定义一个名为initPwd的箭头函数
  const initPwd = () => {
    // 获取selKeys的长度
    const len = selKeys.length;
    // 如果长度为0，则显示提示信息并返回
    if (len === 0) {
      message.info('请选择初始化密码的用户！');
      return;
    }
    const userIDs = selKeys as number[];
    Modal.confirm({
      title: '提示',
      content: `你确认要将所选员工的密码进行初始化吗？`,
      okText: '确定',
      cancelText: '取消',
      onOk: () => {
        initPassword(userIDs)
          .then(function () {
            message.success('密码初始化成功！');
          })
          .catch(function (ex) {
            console.log(ex);
          });
      },
    });
  };
  // 定义一个名为userOut的箭头函数
  const userOut = () => {
    // 获取选中项的长度
    const len = selKeys.length;
    // 如果长度为0，则提示选择要离职的员工并返回
    if (len === 0) {
      message.info('请选择要离职的员工！');
      return;
    }
    // 将选中的键转换为数字数组
    const userIDs = selKeys as number[];
    // 弹出确认框
    Modal.confirm({
      // 设置标题
      title: '提示',
      // 设置内容
      content: `你确认要将所选员工离职登出吗？`,
      // 设置确定按钮文本
      okText: '确定',
      // 设置取消按钮文本
      cancelText: '取消',
      // 确定按钮的点击事件
      onOk: () => {
        // 调用leaveOffice函数进行员工离职
        leaveOffice(userIDs)
          // 如果离职成功，显示成功消息
          .then(function () {
            message.success('离职登出成功！');
          })
          // 如果出现异常，打印异常信息
          .catch(function (ex) {
            console.log(ex);
          });
      },
    });
  };
  const controlsImport: API.Control[] = [
    { name: 'No', type: 'text', label: '员工编号', grid: 12 / 24 },
    { name: 'Name', type: 'text', label: '员工名称', grid: 12 / 24 },
    { name: 'Sex', type: 'text', label: '性别', grid: 12 / 24 },
    { name: 'Memo', type: 'text', label: '所属部门', grid: 12 / 24 },
  ];
  const butt = (
    <>
      <Button key="initPwd" onClick={() => initPwd()}>
        <IconFont type="icon-shanchu" />
        初始化密码
      </Button>
      <Button key="initPwd" onClick={() => userOut()}>
        <IconFont type="icon-shanchu" />
        离职登出
      </Button>
    </>
  );
  return (
    <CommonPage
      daoName={daoName}
      columns={columns}
      controls={controls}
      title="员工信息"
      extraButtons={butt}
      importName={importName}
      importControls={controlsImport}
      importMode="InsertOnly"
      keyColName="ID"
      importTemplateName="员工信息导入模板"
      onSelectChange={async (keys) => setSelKeys(keys)}
    />
  );
};

export default Users;
